package cc.mrbird.febs.weixin.share;

import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.customer.entity.Customer;
import cc.mrbird.febs.customer.service.ICustomerService;
import cc.mrbird.febs.weixin.WeiXinConstant;
import cc.mrbird.febs.weixin.WeiXinUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ Author 马超伟
 * @ Date 2020-11-03 17:26
 * @ Description:
 * @ Version:
 */
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping("/api")
public class jsapi extends BaseController {

    @Resource
    private ICustomerService customerService;

    /**
     * @ Author: 马超伟
     * @ Date: 2020/11/3 17:46
     * @ Params: [url]
     * @ return: cc.mrbird.febs.common.entity.FebsResponse
     * @ Description: 获取微信签名所用的数据
     */
    @CrossOrigin
    @RequestMapping(value="/getSignature" ,method = {RequestMethod.POST, RequestMethod.GET})
    public FebsResponse getSignature(String url,Integer customerId){
        String accessToken = null;
        String signature = null;
        accessToken = WeiXinUtil.getToken(customerId);
        String jsTicket = WeiXinUtil.getJsTicket(accessToken,customerId);
        String timestamp = WeiXinUtil.getTimestamp();
        String nonceStr = WeiXinUtil.getNonceStr();
        signature = getSignature(timestamp, nonceStr, url, jsTicket);
        FebsResponse response = new FebsResponse().message("生成签名成功！");
        response.put("timestamp",timestamp);
        response.put("nonceStr", nonceStr);
        response.put("appId", WeiXinConstant.AppId);
        response.put("signature",signature);


        try {
            //提取微信分享的pid,并且设置关联关系
            String[] param = url.split("[?]")[1].split("&");
            for (String s : param) {
                if ("pid".equals(s.split("[=]")[0])) {
                    customerService.update(new UpdateWrapper<Customer>().lambda()
                            .eq(Customer::getCustomerId, customerId)
                            .set(Customer::getPid, s.split("[=]")[1]));
                }
            }
        } catch (Exception e) {
            //首次进入！！！
            System.out.println("首次进入，不用分享");
        }
        return response;
    }

    /**
     * @ Author: 马超伟
     * @ Date: 2020/11/3 17:37
     * @ Params: [timestamp, nonceStr, url, jsapiTicket]
     * @ return: java.lang.String
     * @ Description: 生成签名
     */
    public static String getSignature(String timestamp, String nonceStr, String url, String jsapiTicket) {
        String string1 = "jsapi_ticket=" + jsapiTicket
                + "&noncestr=" + nonceStr + "&timestamp=" + timestamp + "&url="
                + url;
        //进行sha1加密
        String digestOfString = new SHA1().getDigestOfString(string1.getBytes());
        return digestOfString;
    }


}